﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Transformer.iHunter.UI
{
    public partial class FilterCommonSettingsPanel : UserControl
    {
        private SelectFolderDialog selectFolderDialog = null;

        private LocalClient client = null;

        public FilterCommonSettingsPanel()
        {
            InitializeComponent();
        }

        public LocalClient Client
        {
            get
            {
                return client;
            }

            set
            {
                client = value;
                if (selectFolderDialog != null)
                {
                    selectFolderDialog.Dispose();
                    selectFolderDialog = null;
                }

                if (client != null)
                {
                    selectFolderDialog = new SelectFolderDialog(client);
                }
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string FilterName
        {
            get
            {
                return this.textBoxName.Text;
            }

            set
            {
                this.textBoxName.Text = value;
            }
        }

        public IFolder MoveToFolder
        {
            get
            {
                return (checkBoxMoveTo.Enabled && checkBoxMoveTo.Checked)
                    ? selectFolderDialog.SelectedFolder
                    : null;
            }

            private set
            {
                if (value == null)
                {
                    this.linkLabelMoveTo.Text = "(choose...)";
                }
                else
                {
                    this.checkBoxMoveTo.Checked = true;
                    this.linkLabelMoveTo.Text = value.Name;
                }
            }
        }

        public Guid MoveToFolderGuid
        {
            get
            {
                IFolder folder = MoveToFolder;
                return (folder == null) ? Guid.Empty : folder.Guid;
            }

            set
            {
                MoveToFolder = (value == Guid.Empty)
                    ? null
                    :selectFolderDialog.SelectFolder(value);
            }
        }

        public bool IsDelete
        {
            get
            {
                return checkBoxDelete.Enabled && checkBoxDelete.Checked;
            }
            set
            {
                checkBoxDelete.Checked = value;
                if (value)
                {
                    this.checkBoxMoveTo.Enabled = false;
                    this.linkLabelMoveTo.Enabled = false;
                    this.checkBoxRead.Enabled = false;
                    this.checkBoxUnread.Enabled = false;
                    this.checkBoxSpam.Enabled = false;
                    this.checkBoxNonSpam.Enabled = false;
                }
                else
                {
                    this.checkBoxMoveTo.Enabled = true;
                    this.linkLabelMoveTo.Enabled = true;
                    this.checkBoxRead.Enabled = true;
                    this.checkBoxUnread.Enabled = true;
                    this.checkBoxSpam.Enabled = true;
                    this.checkBoxNonSpam.Enabled = true;
                }
            }
        }

        public bool IsMarkAsRead
        {
            get
            {
                return checkBoxRead.Enabled && checkBoxRead.Checked;
            }
            set
            {
                checkBoxRead.Checked = value;
            }
        }

        public bool IsMarkAsUnread
        {
            get
            {
                return checkBoxUnread.Enabled && checkBoxUnread.Checked;
            }
            set
            {
                checkBoxUnread.Checked = value;
            }
        }

        public bool IsMarkAsSpam
        {
            get
            {
                return checkBoxSpam.Enabled && checkBoxSpam.Checked;
            }
            set
            {
                checkBoxSpam.Checked = value;
            }
        }

        public bool IsMarkAsNonSpam
        {
            get
            {
                return checkBoxNonSpam.Enabled && checkBoxNonSpam.Checked;
            }
            set
            {
                checkBoxNonSpam.Checked = value;
            }
        }

        public FilterAction Action
        {
            get
            {
                FilterAction action = FilterAction.None;
                if (IsDelete)
                {
                    action |= FilterAction.Delete;
                }
                if (IsMarkAsRead)
                {
                    action |= FilterAction.MarkAsRead;
                }
                if (IsMarkAsSpam)
                {
                    action |= FilterAction.MarkAsSpam;
                }
                if (IsMarkAsNonSpam)
                {
                    action |= FilterAction.MarkAsNonspam;
                }
                if (MoveToFolder != null)
                {
                    action |= FilterAction.MoveToFolder;
                }

                return action;
            }
        }

        private void linkLabelMoveTo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (selectFolderDialog.ShowDialog() == DialogResult.OK)
            {
                MoveToFolder = selectFolderDialog.SelectedFolder;
            }
        }

        private void checkBoxDelete_CheckedChanged(object sender, EventArgs e)
        {
            IsDelete = checkBoxDelete.Checked;
        }

        private void checkBoxRead_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxRead.Checked)
            {
                this.checkBoxUnread.Checked = false;
            }
        }

        private void checkBoxUnread_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxUnread.Checked)
            {
                this.checkBoxRead.Checked = false;
            }
        }

        private void checkBoxSpam_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxSpam.Checked)
            {
                this.checkBoxNonSpam.Checked = false;
            }
        }

        private void checkBoxNonSpam_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxNonSpam.Checked)
            {
                this.checkBoxSpam.Checked = false;
            }
        }

        private void checkBoxMoveTo_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxMoveTo.Checked &&
                selectFolderDialog.ShowDialog() == DialogResult.OK)
            {
                MoveToFolder = selectFolderDialog.SelectedFolder;
            }
        }

        public void ResetControlValues(FilterInfo settings)
        {
            FilterName = settings.Name;
            IsDelete = ((settings.Action & FilterAction.Delete) != 0);
            IsMarkAsRead = ((settings.Action & FilterAction.MarkAsRead) != 0);
            IsMarkAsSpam = ((settings.Action & FilterAction.MarkAsSpam) != 0);
            IsMarkAsNonSpam = ((settings.Action & FilterAction.MarkAsNonspam) != 0);
            MoveToFolderGuid = ((settings.Action & FilterAction.MoveToFolder) == 0)
                ? Guid.Empty
                : settings.FolderGuid;
        }
    }
}
